<?php

namespace App\Http\Controllers\Reception\Article;

use App\Models\Article;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Http\Request;

//文章模块
class ArticleController extends Controller
{
    //获取文章列表
    public function getArticleList(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifytokens($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        //翻页
        $pagenNum = (int) $request->query('page');
        $pageSize = (int) $request->query('pageSize');
        if ($pageSize > 50) {
            return false;
        }
        $page = $pagenNum - 1;
        if ($page != 0) {
            $page = $pageSize * $page;
        }

        $count = Article::leftjoin('upload_file', 'article.fileid', '=', 'upload_file.fileid')->count();
        if ($request->query('catid')) {
            $map['catid'] = (int) $request->query('catid');
            $info = Article::leftjoin('upload_file', 'article.fileid', '=', 'upload_file.fileid')
                ->offset($page)
                ->limit($pageSize)
                ->where($map)
                ->orderByDesc('article.article_create_time')
                ->select('article.aid', 'article.catid', 'article.article_title', 'article.article_introduction', 'article.article_create_time', 'article.article_read_num', 'article.article_comment_num', 'upload_file.file_name', 'upload_file.url')
                ->get();
        } else {
            $info = Article::leftjoin('upload_file', 'article.fileid', '=', 'upload_file.fileid')
                ->offset($page)
                ->limit($pageSize)
                ->orderByDesc('article.article_create_time')
                ->select('article.aid', 'article.catid', 'article.article_title', 'article.article_introduction', 'article.article_create_time', 'article.article_read_num', 'article.article_comment_num', 'upload_file.file_name', 'upload_file.url')
                ->get();
        }

        if ($info) {
            $info = json_decode($info, true); //对象转成数组
            foreach ($info as $key => $value) {
                $info[$key]['url'] =  http_type() . $_SERVER['HTTP_HOST'] . "/" . $value['url'] . "/" . $value['file_name'];
                $info[$key]['article_create_time'] = date('Y-m-d', $value['article_create_time']);
            }
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        $data['count'] = $count;
        return $data;
    }

    //获取文章详情
    public function getArticleDetail(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifytokens($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['aid'] = (int) $request->query('aid');
        $user_id = (int) $request->query('user_id');
        if ($user_id) {
            $this->inReadNum($map['aid'], $user_id);
        }
        $info = Article::where($map)
            ->leftjoin('upload_file', 'article.fileid', '=', 'upload_file.fileid')
            ->select('article.aid', 'article.catid', 'article.article_title', 'article.article_introduction', 'article.article_author', 'article.article_content', 'article.article_create_time', 'article.article_create_time', 'article.article_read_num', 'article.article_comment_num', 'upload_file.file_name', 'upload_file.url')

            ->first();
        if ($info) {
            $info->url =  http_type() . $_SERVER['HTTP_HOST'] . "/" . $info->url . "/" . $info->file_name;
            $info->article_create_time = date('Y-m-d', $info->article_create_time);
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }

    //更新文章真实阅读量
    public function inReadNum($aid = 0, $user_id = 0)
    {
        $map['aid'] = (int) $aid;
        if (!$user_id) {
            return false;
        }
        $cacheReadNum = $map['aid'] . $user_id . 'article_read_num';
        if (!Cache::has($cacheReadNum)) {
            Cache::put($cacheReadNum, $cacheReadNum, 3600);
            $msg = Article::where($map)->increment('article_read_num', 1);
        }
    }

    //搜索文章
    public function searchArticle(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifytokens($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        //翻页
        $pagenNum = (int) $request->input('page');
        $pageSize = (int) $request->input('pageSize');
        if ($pageSize > 50) {
            return false;
        }
        $page = $pagenNum - 1;
        if ($page != 0) {
            $page = $pageSize * $page;
        }
        $count = Article::leftjoin('upload_file', 'article.fileid', '=', 'upload_file.fileid')->count();
        $map['article_title'] = htmlspecialchars(addslashes($request->input('keyword')));
        $info = Article::where('article_title', 'like', '%' . $map['article_title'] . '%')
            ->leftjoin('upload_file', 'article.fileid', '=', 'upload_file.fileid')
            ->offset($page)
            ->limit($pageSize)
            ->get();
        if ($info) {
            $info = json_decode($info, true); //对象转成数组
            foreach ($info as $key => $value) {
                $info[$key]['url'] =  http_type() . $_SERVER['HTTP_HOST'] . '/uploads/images/' . date('Y-m-d', $value['create_flie_time']) . "/" . $value['file_name'];
                $info[$key]['article_create_time'] = date('Y-m-d', $value['article_create_time']);
            }
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        $data['count'] = $count;
        return $data;
    }

    //文章点赞
    public function ArticlePraise(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifytokens($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['user_id'] = (int) $request->query('user_id');
        $map['aid'] = (int) $request->query('aid');
        if(!$map['user_id']||!$map['aid']){
            $data=fail();
            $data['msg']='参数错误';
            return $data;
        }
        $info = DB::table('article_praise')->where($map)->first();
        if ($info) {
            if ($info->article_praise_type) {
                $date['article_praise_type'] = 0;
                $isPraise = false;
                $msg = DB::table('article_praise')
                    ->where($map)
                    ->update($date);
            } else {
                $date['article_praise_type'] = 1;
                $isPraise = true;
                $msg = DB::table('article_praise')
                    ->where($map)
                    ->update($date);
            }
        } else {
            if ($map['user_id'] && $map['aid']) {
                $date['user_id'] = $map['user_id'];
                $date['aid'] = $map['aid'];
                $date['article_praise_type'] = 1;
                $date['ip']=get_real_ip();
                $date['article_praise_create_time'] = time();
                $msg = DB::table('article_praise')->insert($date);
                $isPraise = true;
            }
        }
        if ($msg) {
            $data = success();
            $data['isPraise'] = $isPraise;
        } else {
            $data = fail();
        }
        return $data;
    }

    //获取点赞文章列表
    public function getArticlePraise(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifytokens($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }

        //翻页
        $pagenNum = (int) $request->query('page');
        $pageSize = (int) $request->query('pageSize');
        if ($pageSize > 50) {
            return false;
        }
        $page = $pagenNum - 1;
        if ($page != 0) {
            $page = $pageSize * $page;
        }
        // $map['article_praise.article_praise_type'] = 1;
        $info = DB::table('article_praise')
            ->leftjoin('article', 'article_praise.aid', '=', 'article.aid')
            ->leftjoin('upload_file', 'article.fileid', '=', 'upload_file.fileid')
            // ->where($map)
            // ->leftjoin('article', 'article_praise.aid ', '=', 'article.aid')
            // ->leftjoin('upload_file', 'article.fileid', '=', 'upload_file.fileid')
            ->offset($page)
            ->limit($pageSize)
            ->get();
        if ($info) {
            $info=json_decode($info,true);
            foreach ($info as $key => $value) {
                $info[$key]['url'] =  http_type() . $_SERVER['HTTP_HOST'] . "/" . $value['url'] . "/" . $value['file_name'];
                $info[$key]['article_create_time'] = date('Y-m-d', $value['article_create_time']);
            }
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }

    //获取文章点赞用户列表
    public function getUserArticlePraise(Request $request){
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifytokens($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['article_praise.aid'] = (int) $request->query('aid');
        $map['article_praise.article_praise_type'] = 1;
        $info = DB::table('article_praise')
        ->where($map)
        ->leftjoin('article', 'article_praise.aid', '=', 'article.aid')
        ->leftJoin('user','article_praise.user_id', '=', 'user.user_id')
        ->select('user.nickName','user.avatarUrl')
        ->get();
        if ($info) {
            $info=json_decode($info,true);
            $info=array_unset_dr($info,'nickName');
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }

    //用户投稿
    public function Contribution(Request $request){
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifytokens($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        
    }
}
